package com.video.user.controller;


import cn.hutool.json.JSONUtil;
import com.video.api.dto.InsertVideoDto;
import com.video.common.config.RestHighLeveConfig;
import com.video.common.enumeration.AppHttpCodeEnum;
import com.video.common.result.PageResult;
import com.video.common.result.ResponseResult;
import com.video.user.domain.dto.UpdateVideoCountDto;
import com.video.user.domain.dto.QueryPageDto;
import com.video.user.domain.dto.VideoDto;
import com.video.user.domain.entity.VideoList;
import com.video.user.service.IVideoListService;
import lombok.RequiredArgsConstructor;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author la
 * @since 2024-11-09
 */
@RestController
@RequestMapping("/video-list")
@RequiredArgsConstructor
public class VideoListController {
    private final RestHighLeveConfig restHighLeveConfig;

    private final IVideoListService videoListService;
    private final RestHighLevelClient client;

    /*
     * 新增或编辑视频
     * */
    @PostMapping("/insert")
    public ResponseResult insert(@RequestBody InsertVideoDto dto) throws IOException {
        videoListService.InsertList(dto);
        //查询索引库是否存在
        GetIndexRequest request = new GetIndexRequest("videolist");
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        if(exists){
            IndexRequest videolist = new IndexRequest("videolist").id(dto.getId().toString());
            videolist.source(JSONUtil.toJsonStr(dto), XContentType.JSON);
            client.index(videolist, RequestOptions.DEFAULT);
        }else{
            // 创建索引库
            client.indices().create(new CreateIndexRequest("videolist"), RequestOptions.DEFAULT);
            // 创建或更新es数据源
            UpdateRequest updateRequest = new UpdateRequest("videolist", dto.getId().toString());
        }


        return ResponseResult.okResult("操作成功");
    }

    /*
     * 分页查询视频列表
     * */
    @GetMapping("/page")
    public ResponseResult queryList(QueryPageDto dto) {
        PageResult result = videoListService.queryVideoList(dto);
        return ResponseResult.okResult(result);
    }
    /**
     * 删除视频
     */
    @DeleteMapping("/delete/{id}")
    public ResponseResult delete(@PathVariable("id") Long id) {
        videoListService.removeVideoById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /*
     * 新增视频点赞数量
     * */
    @GetMapping("/updateVideoCount")
    public ResponseResult updateVideoCount(UpdateVideoCountDto dto) {

        videoListService.updateVideoCount(dto);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 查看视频信息
     */
    @GetMapping("/info/{id}")
    public ResponseResult info(@PathVariable("id") Long id) {
        VideoList videoList = videoListService.getById(id);
        return ResponseResult.okResult(videoList);
    }


    /**
     * es查询视频信息
     */
    @GetMapping("/searchList")
    public ResponseResult search(VideoDto keywords) throws IOException {
        RestHighLevelClient highLevelClient = restHighLeveConfig.restHighLevelClient();
        PageResult result = videoListService.searchList(keywords, highLevelClient);
        return ResponseResult.okResult(result);
    }
}
